package com.duleme.service.impl;

import com.duleme.common.utils.sdk.CCPRestSDK;
import com.duleme.service.CaptchaService;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

public class CaptchaServiceImpl implements CaptchaService {
     public String generateCaptcha(){
        Random random=new Random();
        String code="";
        //生成六位随机数
        for(int i=0;i<6;i++){
            int a=random.nextInt(10);
            code+=String.valueOf(a);
        }
        return code;
    }

    public void sendCaptcha(String phonenumber, String code,int expire) {
        HashMap<String, Object> result = null;

        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount("8aaf07086b6526f8016b880756b4167d", "a327b81e6f0d4f68872cb01207d40c5b");// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId("8aaf07086b6526f8016b8807570d1683");// 初始化应用ID
        result = restAPI.sendTemplateSMS(phonenumber,"1" ,new String[]{code,String.valueOf(expire)});

        System.out.println("SDKTestSendTemplateSMS result=" + result);

        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
    }

    public static void main(String[] args){
//        for(int i=0;i<10;i++){
//            int a=new Random().nextInt(10);
//            System.out.println(a);
//        }
//        CaptchaServiceImpl captchaService=new CaptchaServiceImpl();
//        Captcha c=captchaService.CaptchaGeneric();
//        System.out.println(c.getCode());
        CaptchaServiceImpl captchaServiceImpl=new CaptchaServiceImpl();
        System.out.println(captchaServiceImpl.generateCaptcha());
    }
}
